package com.gopay.bis.service.bank.impl.ebank;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.manager.bank.ebank.KuaiQianManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.KuaiQianConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.kuaiqian.ebank.KuaiQianMessage;
import com.gopay.common.domain.bank.kuaiqian.ebank.KuaiQianQueryMessage;
import com.gopay.common.domain.bank.kuaiqian.ebank.KuaiQianQueryResultMessage;
import com.gopay.common.domain.bank.kuaiqian.ebank.KuaiQianResultMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankInteractRemoteService;
import com.ibm.icu.math.BigDecimal;

public class KuaiQianBankServiceImpl implements BankInteractRemoteService {

	@Resource(name = "kuaiQianManager")
	private KuaiQianManager kuaiQianManager;

	@Override
	public Result bankBack(BaseMessage baseMessage) {
		Result result = new Result(false);
		KuaiQianResultMessage kuaiQianResultMessage = (KuaiQianResultMessage) baseMessage;
        boolean flag = false;
        try {
            flag = (Boolean)kuaiQianManager.verify(kuaiQianResultMessage);
            result.setModel("message", kuaiQianResultMessage);
            result.setDefaultModel(flag);
            result.setSuccess(true);
            // 验签成功入账
            if (flag) {
                // 银行返回成功状态
                if (KuaiQianConstants.PRO_ORDER_SUCCESS.equals(kuaiQianResultMessage.getPayResult())) {
                    Map<String, String> param = new HashMap<String, String>();
                    param.put("orderId", baseMessage.getOrderId());
                    param.put("amt", new BigDecimal(kuaiQianResultMessage.getPayAmount()).movePointLeft(2).toString());
                    param.put("cardType", "Unknown");
                    param.put("notifyNum", "1");
                    param.put("payStatus", "00");
                    param.put("retCode", "00");
                    param.put("retMsg", "验签成功");
                    param.put("signData", kuaiQianResultMessage.getSignMessage());
                    param.put("isFront", baseMessage.isFront()+"");
                    result.setModel("isNotifyCashier", param);
                } else {
                    // 通知后台地址该订单失败
                    Map<String, String> param = new HashMap<String, String>();
                    param.put("orderId", baseMessage.getOrderId());
                    param.put("amt",  new BigDecimal(kuaiQianResultMessage.getPayAmount()).movePointLeft(2).toString());
                    param.put("cardType", "Unknown");
                    param.put("notifyNum", "1");
                    param.put("payStatus", "01");
                    param.put("retCode", "01");
                    param.put("retMsg", "验签失败");
                    param.put("signData", kuaiQianResultMessage.getSignMessage());
                    param.put("isFront", baseMessage.isFront()+"");
                    result.setModel("isNotifyCashier", param);
                }
            }
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
        }
        return result;
	}

	@Override
	public Result dayend(BaseMessage arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result pay(BaseMessage baseMessage) {
		Result result = new Result(false);
		try {
			KuaiQianMessage message = new KuaiQianMessage();
			BeanUtils.copyProperties(baseMessage,message);
			KuaiQianMessage resultMsg = (KuaiQianMessage)kuaiQianManager.sign(message);
			result.setDefaultModel(resultMsg);
			result.setSuccess(true);
		} catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
    		result.setResultCode(e.getErrCode());
		}catch(Exception ex){
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6029);
		}
		return result;
	}

	@Override
	public Result query(BaseMessage baseMessage) {
		Result result = new Result(false);
		KuaiQianQueryMessage queryMessage = (KuaiQianQueryMessage) baseMessage;
        try {
        	KuaiQianQueryResultMessage orderRes = (KuaiQianQueryResultMessage)kuaiQianManager.query(queryMessage);
	        result.setDefaultModel(orderRes);
	        result.setSuccess(true);	        
		}catch (GopayException e) {
			BankLogUtils.printServiceImplExceptionLog(e);
    		result.setResultCode(e.getErrCode());
		}catch(Exception ex){
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
		}
        return result;
	}

	@Override
	public Result refund(BaseMessage arg0) {
		return null;
	}

	@Override
	public Result queryBack(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

    @Override
    public Result refundBack(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Result refundQuery(BaseMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

	@Override
	public Result sendBankRefund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

}
